package jp.co.dailyReport.workItem.sql;

import java.util.Calendar;
import java.sql.Date;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.workItem.WorkItem;
import jp.co.dailyReport.Utilities;

/**
 * 作業項目情報を登録するクラスです。
 */
public class WorkItemWeeklyReportInsertExecuter implements Executer {

	private static Logger log = Logger.getLogger(WorkItemWeeklyReportInsertExecuter.class);
	private WorkItem workItem;

	public WorkItemWeeklyReportInsertExecuter(WorkItem workItem) {
		this.workItem = workItem;
	}

	/**
	 * 作業項目情報を登録します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "insert into workItem (displayNm,projectId,name,deadline,addStaffId,upDay) values (?,?,?,?,?,?)";
		log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, workItem.getDisplayNm());
			pstmt.setInt(2, workItem.getProjectId());
			pstmt.setString(3, Utilities.convUniqueCharacter(workItem.getName()));
			pstmt.setDate(4, new Date(workItem.getDeadline().getTime().getTime()));
			pstmt.setInt(5, workItem.getAddStaffId());
			Calendar cal = Calendar.getInstance();
			pstmt.setDate(6, new Date(cal.getTime().getTime()));
			pstmt.execute();
		} finally {
			if(pstmt != null)
			 pstmt.close();
		}
		return null;
	}
}
